package com.hpjpw.idoapp.service;

import com.hpjpw.ido.domain.CheerComment;
import com.hpjpw.ido.domain.CheerMoment;
import com.hpjpw.ido.domain.CheerThumb;
import com.hpjpw.ido.domain.Topic;
import com.hpjpw.ido.service.ICheerCommentService;
import com.hpjpw.ido.service.ICheerMomentService;
import com.hpjpw.ido.service.ICheerThumbService;
import com.hpjpw.ido.service.ITopicService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IDoCheerService {
    @Autowired
    ICheerMomentService cheerMomentService;
    @Autowired
    ICheerThumbService cheerThumbService;
    @Autowired
    ICheerCommentService cheerCommentService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ITopicService topicService;

    /* 将所有返回动态相关信息存到map中 */
    public Map<String, Object> momentMap(CheerMoment item){
        Map<String, Object> map = new HashMap<>();
        /* 返回动态id，动态发布者id/姓名/头像，动态主题内容，动态内容及图片，动态发布时间 */
        map.put("id", item.getId());
        map.put("reportId", item.getReportId());
        map.put("reportName", sysUserService.selectUserById(item.getReportId()).getNickName());
        map.put("reportAvatar", sysUserService.selectUserById(item.getReportId()).getAvatar());
        /* 没有主题则传null值 */
        Long topicId = item.getTopicId();
        if (StringUtils.isNotNull(topicId)) {
            Topic topic = topicService.selectTopicById(topicId);
            if (StringUtils.isNotNull(topic)) {
                map.put("topic", topic.getContent());
            } else {
                map.put("topic", null);
            }
        } else {
            map.put("topic", null);
        }
        map.put("content", item.getContent());
        map.put("picture1", item.getPicture1());
        map.put("picture2", item.getPicture2());
        map.put("picture3", item.getPicture3());
        map.put("time", item.getTime());
        /* 返回所有点赞id，点赞者id和姓名，以及点赞总数量 */
        List<Map<String, String>> thumbList = new ArrayList<>();
        CheerThumb cheerThumb = new CheerThumb();
        cheerThumb.setMomentId(item.getId());
        List<CheerThumb> cheerThumbList = cheerThumbService.selectCheerThumbList(cheerThumb);
        for (CheerThumb thumb : cheerThumbList) {
            Map<String, String> mapOfThumb = new HashMap<>();
            mapOfThumb.put("thumbId", thumb.getId());
            mapOfThumb.put("thumbedId", thumb.getThumbedId().toString());
            mapOfThumb.put("thumbedName", thumb.getThumbedName());
            thumbList.add(mapOfThumb);
        }
        map.put("thumbNum", cheerThumbList.size());
        map.put("thumbs", thumbList);
        /* 返回所有评论，具体评论id、评论者id、名称、头像、评论内容以及总评论数 */
        List<Map<String, String>> commentList = new ArrayList<>();
        CheerComment cheerComment = new CheerComment();
        cheerComment.setMomentId(item.getId());
        List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerComment);
        for (int i = 0; i < cheerCommentList.size(); i++) {
            IDoPersonalMomentService.getCheerComment(commentList, cheerCommentList, i, sysUserService);
        }
        map.put("commentNum", cheerCommentList.size());
        map.put("comments", commentList);
        return map;
    }

    /* 针对前端的加油站动态请求服务，一次性返回相关数据 */
    public List<Map<String, Object>> momentListResponse() {
        /* 实际返回的列表 */
        List<Map<String, Object>> listReturn = new ArrayList<>();
        /* 获取所有动态 */
        CheerMoment cheerMoment = new CheerMoment();
        cheerMoment.setStatus("1");
        List<CheerMoment> cheerMomentList = cheerMomentService.selectCheerMomentList(cheerMoment);
        System.out.println("cheerMomentList=>" + cheerMomentList);
        /* 对每一条加油站动态插入map数据 */
        for (CheerMoment item : cheerMomentList) {
            listReturn.add(momentMap(item));
        }
        /* 对结果做一次反转，即按照最近的时间排序，并将此结果返回 */
        /*Collections.reverse(listReturn);*/
        return listReturn;
    }

    /* 针对前端的撕逼墙具体动态的评论请求服务 */
    public List<Map<String, String>> commentListResponse(Long momentId) {
        CheerComment cheerComment = new CheerComment();
        cheerComment.setMomentId(momentId);
        List<CheerComment> cheerCommentList = cheerCommentService.selectCheerCommentList(cheerComment);
        List<Map<String, String>> listReturn = new ArrayList<>();
        /* 返回每一条评论的动态id，评论id，评论内容，评论者id，评论者姓名，评论者头像 */
        for (CheerComment item : cheerCommentList) {
            Map<String, String> map = new HashMap<>();
            map.put("momentId", item.getMomentId().toString());
            map.put("commentId", item.getId().toString());
            map.put("content", item.getContent());
            map.put("commentatorId", item.getFromId().toString());
            map.put("commentatorName", sysUserService.selectUserById(item.getFromId()).getNickName());
            map.put("commentatorAvatar", sysUserService.selectUserById(item.getFromId()).getAvatar());
            listReturn.add(map);
        }
        return listReturn;
    }

    /* 针对某一动态id(此处确定存在此动态，controller中添加安全校验)，返回该动态所有内容 */
    public Map<String, Object> momentResponse(Long momentId) {
        Map<String, Object> map = new HashMap<>();
        CheerMoment item = cheerMomentService.selectCheerMomentById(momentId);
        /* 审核通过 */
        if (item.getStatus().equals("1")) {
            map = momentMap(item);
        }
        return map;
    }
}
